<?php
/**
 * Created by PhpStorm.
 * User: Keter
 * Date: 2016/8/12
 * Time: 9:51
 */
class GetVisitDetail extends ApiRedisBase
{
    public $visitRedis;

    public function logic()
    {
        $this->checkUser();

        $num = $this->params['visitNum'];
        $this->visitRedis = $this->redis('visit_log');

        if (empty($num)) {
            $this->returnError('empty_visit_num');
        }

        if (!$this->visitRedis->exists('V:' . $num)) {
            $this->returnError('no_visit_num');
        }

        //获取回放详情
        $data = $this->getPlaybackDetail($num);

        $this->returnData('', $data);
    }

    /**
     * 获取回放详情
     * @param $num
     */
    public function getPlaybackDetail($num)
    {
        $result = array();
        $key = 'VNL:' . $num;

        if ($this->visitRedis->exists($key)) {
            $visitInfo = $this->visitRedis->hMget($key, array('uids', 'detail', 'paiZuID', 'seq'));
            $visitInfo['detail'] = json_decode($visitInfo['detail'], true);
            $visitInfo['uids'] = json_decode($visitInfo['uids'], true);

            $score = [];
            if (isset($visitInfo['detail']['score'])) {
                foreach ($visitInfo['detail']['score'] as $key => $row) {
                    $score[] = $row[1];
                }
                $result = [
                    'nicknames' => $this->getNickname($visitInfo),
                    'list' => [$score],
                    'paiZuID' => $visitInfo['paiZuID'],
                    'seq' => $visitInfo['seq'],
                ];
            }
        }

        return $result;
    }

    /**
     * 获取用户昵称
     * @param $uids
     * @return array
     */
    public function getNickname($row)
    {
        $result = array();
        $redis = $this->redis('user_info');

        foreach ($row['uids'] as $key => $uid) {
            $userInfo = $redis->hMget('U:' . $uid, array('nickname'));
            if ($userInfo) {
                $result[$key] = $userInfo['nickname'];
            } else {
                $result[$key] = '';
            }
        }

        return $result;
    }
}